package pers.sen.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import pers.sen.health.general.EmptyUtil;
import pers.sen.health.mapper.UserMapper;
import pers.sen.health.po.Permission;
import pers.sen.health.po.Role;
import pers.sen.health.po.User;
import pers.sen.health.service.RoleService;
import pers.sen.health.service.UserService;
import pers.sen.health.vo.RoleVO;
import pers.sen.health.vo.UserVO;

import java.util.Set;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/11/13 22:06
 * @Description TODO
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private RoleService roleService;


    @Override
    public UserVO findUserInfoByUsername(String username) {
        // 查询该用户密码
        UserVO userVO = baseMapper.findUserInfoByUsername(username);
        if (EmptyUtil.isNullOrEmpty(userVO)) {
            return null;
        }
        // 查询该用户角色关键字
        Set<RoleVO> roleVOSet = baseMapper.findRoleInfoByUId(userVO.getId());
        for (RoleVO roleVO : roleVOSet) {
            // 查询该用户权限关键字
            Set<Permission> permissions = roleService.findPermissionInfoByRId(roleVO.getId());
            roleVO.setPermissionSet(permissions);
        }
        userVO.setRoles(roleVOSet);

        return userVO;
    }
}
